package com.unionwo.commons.base;



public class PageBarUtil {

	/**
	 * 
	 * @param nowPage 目前页
	 * @param sumPage 总页数
	 * @param bar_num 需要分成数字量
	 * @return
	 */
	public static int[] getPageBarArray(int nowPage, int sumPage, int bar_num) {
		int[] bar = new int[bar_num];
		int mid = 0;// 中间数
		int leftIndex = 0;// 左位移数

		if (bar_num % 2 != 0) {
			leftIndex = (bar_num - 1) / 2; // 左右位移数
			mid = bar_num / 2 + 1;
		} else {
			leftIndex = bar_num / 2 - 1;
			mid = bar_num / 2;
		}

		if (sumPage <= bar_num) { // 总页数小于等于页码数组长度
			for (int i = 0; i < sumPage; i++) {
				bar[i] = i + 1;
			}
		} else if (nowPage + 1 <= mid) {// 当前页小于等于页码数组长度中间数
			for (int i = 0; i < bar_num; i++) {
				if (i < sumPage)
					bar[i] = i + 1;
				else
					break;
			}
		} else if (sumPage - (nowPage + 1) <= mid) {// 当前页在总页码与页码数组中间数中
			int index = 0;
			for (int i = bar_num; i > 0; i--) {
				if ((sumPage - index) > 0) {
					bar[i - 1] = sumPage - index;
					index++;
				} else {
					break;
				}
			}
		} else { // 其他情况
			bar[mid - 1] = nowPage + 1;
			int index = 1;
			for (int i = leftIndex; i > 0; i--) {
				bar[i - 1] = nowPage + 1 - index;
				index++;
			}

			int rightIndex=1;
			for (int i = mid; i < bar_num; i++) {
				bar[index]=nowPage+1+rightIndex;
				rightIndex++;
				index++;
			}

		}

		return bar;
	}

	public static void main(String[] args) throws Exception {
		int[] bar = PageBarUtil.getPageBarArray(13, 56, 9);
		String str = "";
		for (int i = 0; i < bar.length; i++)
			str+="|"+bar[i];
		System.out.println(str);
	}
}
